package com.cy.store.service.impl;

import com.cy.store.entity.Product;
import com.cy.store.mapper.ProductMapper;
import com.cy.store.service.IProductService;
import com.cy.store.service.ex.ProductNotFoundException;
import com.cy.store.vo.ProductCategoryVO;
import com.cy.store.vo.ProductSearchVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<Product> findHotList() {
        List<Product> list = productMapper.findHotList();
        for (Product product : list) {
            product.setPriority(null);
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return list;
    }

    @Override
    public List<Product> getNewProduct() {
        List<Product> list = productMapper.findNewProduct();
        for (Product product : list) {
            product.setPriority(null);
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return list;
    }

    @Override
    public ProductSearchVO productSearch(String searchKeyword, Integer pageNumber) {

        if(pageNumber == null){
            pageNumber = 1;
        }

        Integer pageSize = 12;
        ProductSearchVO vo = new ProductSearchVO();

        Integer count = productMapper.searchPageTotal(searchKeyword);

        Integer totalPage;

        if(count % pageSize == 0){
            totalPage = count / pageSize;
        }else{
            totalPage = count / pageSize + 1;
        }

        List<Product> list = productMapper.searchPageProductList(searchKeyword,(pageNumber - 1) * pageSize ,pageSize);

        vo.setTotalPage(totalPage);
        vo.setProductList(list);
        return vo;
    }

    @Override
    public ProductSearchVO productSearchType(Integer searchType, Integer pageNumber) {
        if(pageNumber == null){
            pageNumber = 1;
        }

        Integer pageSize = 12;
        ProductSearchVO vo = new ProductSearchVO();

        Integer count = productMapper.searchPageTotalWithType(searchType);

        Integer totalPage;

        if(count % pageSize == 0){
            totalPage = count / pageSize;
        }else{
            totalPage = count / pageSize + 1;
        }

        List<Product> list = productMapper.searchPageProductWithTypeList(searchType,(pageNumber - 1) * pageSize ,pageSize);

        vo.setTotalPage(totalPage);
        vo.setProductList(list);
        return vo;
    }

    @Override
    public Product findById(Integer id) {
        Product product = productMapper.findById(id);
        // 判断查询结果是否为null
        if (product == null) {
            throw new ProductNotFoundException("尝试访问的商品数据不存在");
        }
        // 将查询结果中的部分属性设置为null
        product.setPriority(null);
        product.setCreatedUser(null);
        product.setCreatedTime(null);
        product.setModifiedUser(null);
        product.setModifiedTime(null);

        return product;
    }

    @Override
    public List<ProductCategoryVO> productCategory() {

        List<ProductCategoryVO> productCategoryVOS = productMapper.productCategory();
        return productCategoryVOS;
    }
}
